﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class CustomerService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public CustomerService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取客户列表
         */
        public async Task<RequestResponseModel<PageResult<CustomerView>>> GetCustomerList(CustomerSearchDTO customerSearchDTO)
        {
            //筛选
            var query = _context.CustomerView
                .WhereIF(a => a.Code.Contains(customerSearchDTO.Code), !string.IsNullOrEmpty(customerSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(customerSearchDTO.Name), !string.IsNullOrEmpty(customerSearchDTO.Name))
                .WhereIF(a => a.Phone.Contains(customerSearchDTO.Phone), !string.IsNullOrEmpty(customerSearchDTO.Phone))
                ;

            //返回列表
            var customerList = await query
                .OrderBy(a => a.CreateTime)
                .Page(customerSearchDTO.CurrentPage, customerSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<CustomerView>(customerList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增客户
         */
        public async Task<RequestResponseModel<string>> AddCustomer(CustomerAddDTO customerAddDTO)
        {
            //新增数据
            var customer = _mapper.Map<CustomerModel>(customerAddDTO);
            customer.CreateTime = DateTime.Now;
            customer.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Customer.Add(customer);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", customer.Id);
        }

        /**
         * 更新客户
         */
        public async Task<RequestResponseModel<string>> PutCustomer(CustomerUpdateDTO customerUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<CustomerModel>(_context.Customer, customerUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", customerUpdateDTO.Id);
            }

            //编辑数据
            var customer = _mapper.Map(customerUpdateDTO, query);

            //更新表
            _context.Customer.Update(customer);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", customer.Id);
        }

        /**
         * 删除客户
         */
        public async Task<RequestResponseModel<string>> DeleteCustomer(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<CustomerModel>(_context.Customer, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.Customer.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
